
<template>
  <div class="loginbody">
    <div class="logindata">
      <div class="logintext">
        <h2>Motorcycle</h2>
      </div>
      <div class="formdata">
        <el-form ref="loginForm" :rules="rules" :model="loginForm">
          <el-form-item prop="userName">
            <el-input v-model="loginForm.userName" clearable placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item prop="userPwd">
            <el-input
                v-model="loginForm.userPwd"
                clearable
                placeholder="请输入密码"
                show-userPwd
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="tool">
        <div>
          <el-checkbox v-model="checked" @change="remenber"
          >记住密码</el-checkbox>
        </div>
        <div>
          <span class="shou" @click="forgetpas">忘记密码？</span>
        </div>
      </div>
      <div class="butt">
        <el-button type="primary" @click="checkLogin('loginForm')"
        >登录</el-button
        >
        <el-button class="shou" @click="register">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>


export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        userName: "",
        userPwd: "",
      },
      checked: false,
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        userPwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    if(localStorage.getItem("news")){
      this.loginForm=JSON.parse(localStorage.getItem("news"))
      this.checked=true
    }
  },
  methods: {
    checkLogin(formName) {
      //验证登录
      this.$refs[formName].validate((valid) => {

        if (valid) {
          //验证通过发送请求到后台完成数据的保存
          //发送axios请求完成数据的接受
          //调用自己封装的请求实例
          this.$request({
                method: 'get',
                url: '/checkLogin',
                params: this.loginForm

              }, res => {
                console.log("res"+res.data.userName+"0000000")
                //跳转页面
                if (res.data.userName != null) {
                  console.log("进来了")
                  //跳转到index 并且通过路由传参的方式将userName带过去给后面的主界面导航栏用
                  //得到params里面的userName就等于loginForm表单里的userName
                  this.$router.push({name: "index", params: {userName: this.loginForm.userName}});

                }
              }, error => {
                //提示信息框
                console.log(error);
              },8100)
        } else {
          //2)验证不通过
          console.log('error submit!!');
          return false;//意味着表单不提交
        }
      })
    },
    remenber(data){
      this.checked=data
      if(this.checked){
        localStorage.setItem("news",JSON.stringify(this.loginForm))
      }else{
        localStorage.removeItem("news")
      }
    },
    forgetpas() {
      this.$message({
        type:"info",
        message:"功能尚未开发额",
        showClose:true
      })
    },
    register() {},
  },
};
</script>

<style scoped>
.loginbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  background-image: url("~@/assets/摩托.png");
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
  padding-top: 150px;
}

.logintext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.logindata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
  font-weight: bolder;
  font-size: 15px;
  text-align: left;
}

/deep/ .el-button {
  width: 100%;
  margin-bottom: 10px;

} */
</style>


